<template>
	<!-- 注册页面 -->
	<view class="wrap">
		<view class="content">
			<view class="title">欢迎注册晨小书</view>
			<!-- <view class="upload">
				<label for="">请选择头像</label>
				<u-upload :action="action"  max-count="1" v-model="image"></u-upload>
			</view> -->
			<view class="form-input">
				<u-input v-model="name" type="text" :border="border" placeholder="请输入昵称" />
			</view>
			<view class="form-input">
				<u-input v-model="email" type="text" :border="border" placeholder="请输入邮箱" />
			</view>
			<view class="form-input">
				<u-input v-model="psd" :type="type" :border="border" :password-icon="passwordIcon" placeholder="请输入密码" />
			</view>
			<view class="form-input">
				<u-input v-model="psded" :type="type" :border="border" :password-icon="passwordIcon" placeholder="请确认密码" />
			</view>
			<button @tap="submit" :style="[inputStyle]" class="getCaptcha">注册</button>
			<view class="alternative">
				<view class="password" ></view>
				<view class="issue" @click="changeUrl()">已有账号，登陆</view>
			</view>
		</view>
		<statement :text="text"></statement>
	</view>
</template>

<script>
// 导入协议组件
import statement from '../../commponts/statement.vue'
export default {
	components:{
		statement
	},
	data() {
		return {
			email: '',
			psd:'',
			psded:'',
			name:'',
			image:'',
			// 上传图片地址
			action: '',
			type: 'password',
			passwordIcon: true,
			border: true,
			text:"注册代表同意"
		}
	},
	computed: {
		inputStyle() {
			let style = {};
			if(this.$u.test.email(this.email) && this.psd && this.name && this.psded ) {
				style.color = "#fff";
				style.backgroundColor = this.$u.color['warning'];
			}
			return style;
		}
	},
	methods: {
	    async submit() {
			const params={
				name:this.name,
				email:this.email,
				password:this.psd,
				password_confirmation:this.psded
			}
			// 当用户输入前台校验时,请求接口注册
			if(this.$u.test.email(this.email) && this.psd && this.name && this.psded){
				await this.$u.post('/api/auth/register',params).then(res=>{
					console.log(res)
					if(res.statusCode===201){
						uni.showToast({
							title:'恭喜您，成功注册',
							icon:'success'
						})
						setTimeout(() => {
							// 此为uView的方法，详见路由相关文档
							this.$u.route('/pages/auth/loginIn')
						}, 1000)
						
					}else{
						uni.showToast({
							title:'注册失败，稍后重新注册',
							icon:'error'
						})
					}
				}).catch(err=>{
					console.log(err)
				})
			}
		},
		changeUrl(){
			this.$u.route({
				url: 'pages/auth/loginIn'
			})
		}
	}
};
</script>

<style lang="scss" scoped>
	.form-input{
		padding-bottom: 40rpx;
	}
	.u-upload{
		margin: 40rpx 0 !important;
	}
	.u-border-bottom{
		margin-bottom: 50rpx !important;
	}
.wrap {
	font-size: 28rpx;
	.content {
		width: 600rpx;
		margin: 80rpx auto 0;

		.title {
			text-align: left;
			font-size: 60rpx;
			font-weight: 500;
			margin-bottom: 100rpx;
		}
		input {
			text-align: left;
			margin-bottom: 10rpx;
			padding-bottom: 6rpx;
		}
		.tips {
			color: $u-type-info;
			margin-bottom: 60rpx;
			margin-top: 8rpx;
		}
		.getCaptcha {
			background-color: rgb(253, 243, 208);
			color: $u-tips-color;
			border: none;
			font-size: 30rpx;
			padding: 12rpx 0;
			
			&::after {
				border: none;
			}
		}
		.alternative {
			color: $u-tips-color;
			display: flex;
			justify-content: space-between;
			margin-top: 30rpx;
		}
	}
	
}
</style>
